package com.ruoyi.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.JSONObjectPro;
import com.ruoyi.common.core.page.AppPage;
import com.ruoyi.common.core.page.Page;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.YesNoEnum;
import com.ruoyi.system.domain.MessReceive;
import com.ruoyi.system.domain.request.PageParam;
import com.ruoyi.system.service.IMessReceiveService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 描述 APP消息Controller
 * 作者 ZhangPing
 * 创建时间 2023/10/7 15:13
 */
@RestController
@RequestMapping("/app/message")
@Slf4j
public class AppMessageController extends BaseController {

    @Autowired
    private IMessReceiveService messReceiveService;

    @ApiOperation("查询未读消息数量")
    @PostMapping(value = "/getCount")
    public AjaxResult getCount() {
        int unreadMessageCount = messReceiveService.unreadMessageCount(getUserId());
        return success(new JSONObjectPro().put("unReadCount", unreadMessageCount));
    }

    @ApiOperation("分页查询")
    @PostMapping(value = "/getList")
    public AjaxResult selectList(@RequestBody PageParam param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<MessReceive> resultList = messReceiveService.appSelectList();
        PageInfo<MessReceive> page = new PageInfo<>(resultList);
        return success(new AppPage<>(page));
    }

    @ApiOperation("查询消息详情")
    @PostMapping(value = "/getDetail")
    public AjaxResult getDetail(@RequestParam Long id) {
        return success(messReceiveService.getDetail(id));
    }

    @ApiOperation("删除消息")
    @Log(title = "APP消息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(@RequestParam Long id) {
        return success(messReceiveService.updateById(new MessReceive().setId(id).setIsDel(YesNoEnum.YES.getCode())));
    }

    @ApiOperation("批量删除消息")
    @Log(title = "APP消息", businessType = BusinessType.DELETE)
    @PostMapping("/batchRemove")
    public AjaxResult batchRemove(@RequestParam Long[] ids) {
        messReceiveService.update(new MessReceive().setIsDel(YesNoEnum.YES.getCode()), new QueryWrapper<MessReceive>().in("id", ids));
        return success();
    }

}
